﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace RepRapSimulator
{
    public partial class Form1 : Form
    {
        SerialPort port;
        public Form1()
        {
            InitializeComponent();
            port = new SerialPort("COM12");
            port.Open();
            port.ReadTimeout = 20;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            const int BUFFER_SIZE = 255;
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            while (true)
            {
                try
                {
                    bytesRead = port.Read(buffer, 0, BUFFER_SIZE);
                    if (bytesRead > 0)
                    {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < bytesRead; i++ )
                        {
                            builder.Append(String.Format("{0:X} ", (short)buffer[i]));
                        }
                        this.richTextBox1.Text = builder.ToString();
                        break;
                    }
                }
                catch (TimeoutException timeEx)
                {
                }
            }
        }
    }
}
